import Vue from 'vue'
import Vuex from 'vuex'
import qs from 'qs'

Vue.use(Vuex)

export default new Vuex.Store({
    //保存共享数据
    state: {
        total: null,
        routeUrl: {
            bookList: '/borrow/list',
        },
        backEndUrl: {
            borrowBookUrl: '/borrow/add',
            queryBorrowsUrl: '/borrow/queryBorrowsInfo',
            returnBookUrl: '/borrow/return',
            deleteBorrowRecordUrl: '/borrow/delete',
            queryAllBorrowsUrl: '/borrow/queryAllBorrows',
        },
        borrowInfo: {
            id: null,
            userId: null,
            bookId: null,
            borrowTime: null,
            returnTime: null,
            status: null
        },
        borrows: null,
    },
    mutations: {
        setBorrows(state, borrows) {
            state.borrows = borrows;
        }, setTotal(state, total) {
            state.total = total;
        }
    },
    actions: {
        queryBorrows(store, form) {
            return new Promise((resolve) => {
                axios.post(store.state.backEndUrl.queryBorrowsUrl, qs.stringify(form)).then(resp => {
                    if (typeof resp != "undefined") {
                        store.commit('setBorrows', resp.data.list);
                        store.commit('setTotal', resp.data.total);
                    }
                    resolve(resp.data.list); //似乎不resolve就不能再dispatch时 then
                });
            });
        },
        borrowBook(store, bookId) {
            return new Promise((resolve) => {
                axios.post(store.state.backEndUrl.borrowBookUrl, qs.stringify({
                    bookId: bookId
                })).then(resp => {
                    if (resp && resp.data.code === 200) { //写完bookInfo、categoryInfo时，我的后端改成了统一的{code,message,data}返回结果
                        resolve({rs: true});
                    } else //如果成功就返回true，没成功还要把后台返回的message传过去,由于不能resolve多个，故保存为json
                        resolve({rs: false, msg: resp.data.message});
                }).catch(() => {
                    resolve({rs: false});
                });
            });
        },
        returnBook(store, id) {
            return new Promise((resolve) => {
                axios.post(store.state.backEndUrl.returnBookUrl, qs.stringify({
                    id: id
                })).then(resp => {
                    if (resp && resp.data.code === 200) {
                        resolve({rs: true});
                    } else
                        resolve({rs: false, msg: resp.data.message});
                }).catch(() => {
                    resolve({rs: false});
                });
            });
        },
        deleteBorrowRecord(store, id) {
            return new Promise((resolve) => {
                axios.post(store.state.backEndUrl.deleteBorrowRecordUrl, qs.stringify({
                    id: id
                })).then(resp => {
                    if (resp && resp.data.code === 200) {
                        resolve({rs: true});
                    } else
                        resolve({rs: false, msg: resp.data.message});
                }).catch(() => {
                    resolve({rs: false});
                });
            });
        }, queryAllBorrows(store, form) {
            return new Promise((resolve) => {
                axios.post(store.state.backEndUrl.queryAllBorrowsUrl, qs.stringify(form)).then(resp => {
                    resolve(resp.data); //似乎不resolve就不能再dispatch时 then
                });
            });
        },
    },
    modules: {},
    getters: {
        getBorrows(state) {
            return state.borrows;
        },
        getTotal(state) {
            return state.total;
        },
        getBorrowInfo(state) {
            return state.borrowInfo;
        }
    }
})
